<?php

namespace App\Http\Controllers\Admin;

use App\Model\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Services\OSS;

class ArticleController extends Controller
{
    //
    public function add_article(Request $request)
    { //添加文章
        $article = new Article();
        $validate = Validator::make($request->all(['title', 'keywords', 'description', 'image', 'classify_id', 'enabled', 'content']), [
            'title' => 'required',
            'keywords' => 'required',
            'description' => 'required',
            'classify_id' => 'required|numeric',
            'enabled' => 'required|boolean',
            'image' => 'required',
            'content' => 'required',
        ]);
        if ($validate->fails()) {
            return response()->json([
                'code' => 1,
                'msg' => $validate->messages()->first(),
                'data' => []
            ]);
        }
        $result = $article->create($request->input());
        if ($result) {
            return response()->json([
                'code' => 1,
                'msg' => '添加文章成功'
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => '模型出错'
            ]);
        }
    }


    public function get_test(Request $request){
        $file = $request->file('file');
        if($file->isValid()){
            $bucket = 'laravel-video-s';
            $clientName = $file->getClientOriginalName();
            $entension = $file->getClientOriginalExtension();
            $newName = md5(date('ymd').$clientName).'.'.$entension;
            $content_type = mime_content_type($file->getRealPath());
            $path = $file->getRealPath();
           $result =  OSS::publicUpload($bucket,$newName,$path,[
                'ContentType' => $content_type,
            ]);
            $url = OSS::getPublicObjectURL($bucket, $newName);
            if($result){
                return response()->json([
                    'code'=>1,
                    'msg'=>'上传成功',
                    'data'=>$url
                ]);
            }
        }
    }

    public function get_article(Request $request)
    {  //获取文章列表 和 搜索
        $data = $request->all();

        $where = [];

        if (isset($data['datetime']) && !empty($data['datetime'])){
            $where[] = ['created_at', '>=', $data['datetime'][0]];
            $where[] = ['created_at', '<=', $data['datetime'][1]];
        }

        if (isset($data['keywords']) && !empty($data['keywords'])){
            $where[] = ['title', 'like', '%'.$data['keywords'].'%'];
        }


        if(isset($data['enabled']) && is_numeric($data['enabled'])){
            $where[] = ['enabled', '=', $data['enabled']];
        }else{
            $where[] = ['enabled', '=', 1];
        }

        $articles = Article::where($where)->paginate(10);
        if ($articles) {
            return response()->json([
                'code' => 1,
                'msg' => '获取成功',
                'data' => $articles
            ]);
        }
        return response()->json([
            'code' => 0,
            'msg' => '获取失败',
            'data' => []
        ]);
    }

    public function update_article(Request $request)
    {
        $validate = Validator::make($request->all(['article_id', 'title', 'keywords', 'description', 'image', 'classify_id', 'enabled', 'content']), [
            'article_id' => 'required|numeric',
            'title' => 'required',
            'keywords' => 'required',
            'description' => 'required',
            'classify_id' => 'required|numeric',
            'enabled' => 'required|boolean',
            'image' => 'required',
            'content' => 'required',
        ]);
        if ($validate->fails()) {
            return response()->json([
                'code' => 0,
                'msg' => $validate->messages()->first(),
                'data' => []
            ]);
        }
        $id = $request->input('article_id');

        $updateData = [
            'title' => $request->input('title'),
            'classify_id' => $request->input('classify_id'),
            'content' => $request->input('content'),
            'description' => $request->input('description'),
            'enabled' => $request->input('enabled'),
            'image' => $request->input('image'),
            'keywords' => $request->input('keywords')
        ];

        $result = Article::where('id', $id)->update($updateData);

        if ($result) {
            return response()->json([
                'code' => 1,
                'msg' => '修改成功',
            ]);
        }


    }

    public function get_specific_article(Request $request)
    {  //获取单个文章

        $validator = Validator::make($request->all('article_id'), [
            'article_id' => 'required|numeric'
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 0,
                'msg' => $validator->messages()->first(),
                'data' => []
            ]);
        }

        $id = $request->input('article_id');

        $result = Article::find($id);

        if ($result) {
            return response()->json([
                'code' => 1,
                'msg' => '获取成功',
                'data' => $result
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => '获取失败',
                'data' => []
            ]);
        }

    }

    public function cancel_article(Request $request)
    {  //删除文章 软删除

        $validator = Validator::make($request->all('id'), [
            'id' => 'required'
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 0,
                'msg' => $validator->messages()->first(),
                'data' => []
            ]);
        }

        $ids = $request->input('id');

        $arr_id = explode(',', $ids);

        $result = Article::wherein('id', $arr_id)->update(['enabled' => false]);

        if ($result) {
            return response()->json([
                'code' => 1,
                'msg' => '删除成功'
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => '服务器出错'
            ]);
        }


    }

    /**
     * 设置和取消为推荐
     * @param Request $request
     *
     * 传参数为 id
     */
    public function set_recommend(Request $request){

        $validator = Validator::make($request->all(['id','recommend']), [
            'id' => 'required',
            'recommend'=>'required | numeric'
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 0,
                'msg' => $validator->messages()->first(),
                'data' => []
            ]);
        }
        $id = $request->input('id');
        $recommend = $request->input('recommend');

        $update_recommend_flag = $recommend == 1 ? false : true;
        $result = Article::where('id', $id)->update(['is_recommend' => $update_recommend_flag]);

        if($result){
            return response()->json([
               'code'=>1,
               'msg'=>'设置成功'
            ]);
        }else{
            return response()->json([
                'code'=>0,
                'msg'=>'推荐失败'
            ]);
        }
    }

    /**
     * 设置和取消为置顶
     * @param Request $request
     *
     * 传参数为 id
     */
    public function set_top(Request $request){

        $validator = Validator::make($request->all(['id','top']), [
            'id' => 'required',
            'top'=>'required | numeric'
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 0,
                'msg' => $validator->messages()->first(),
                'data' => []
            ]);
        }
        $id = $request->input('id');
        $top = $request->input('top');

        $update_top_flag = $top == 1 ? false : true;
        $result = Article::where('id', $id)->update(['is_top' => $update_top_flag]);

        if($result){
            return response()->json([
                'code'=>1,
                'msg'=>'设置成功'
            ]);
        }else{
            return response()->json([
                'code'=>0,
                'msg'=>'推荐失败'
            ]);
        }
    }


    /**
     * 完全从数据库删除文章
     * @param Request $request
     *
     * 传参数为 id
     */

    public function cancel_completely_article(Request $request){
        $validator = Validator::make($request->all(['id']), [
            'id' => 'required|numeric',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 0,
                'msg' => $validator->messages()->first(),
                'data' => []
            ]);
        }

        $id = $request->input('id');

        $isExist = Article::where('id',$id)->exists();

        if($isExist){
            $article = Article::where('id',$request->input('id'))->delete();
            if($article){
                return response()->json([
                    'code' => 1,
                    'msg' => '删除成功',
                ]);
            }
        }else{
            return response()->json([
                'code' => 0,
                'msg' => '删除的文章不存在',
            ]);
        }

    }


}
